package minigame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.io.*;
import java.net.URL;

import javax.sound.sampled.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.omg.CORBA.PRIVATE_MEMBER;

public class Board extends JPanel implements Runnable, commons {
	private Dimension d;
	private ArrayList giacam_;
	private Player player;
	
	private int giacamX = 150;
	private int giacamY = 5;
	private int direction = -1;
	private int ketqua = 0;
	private int die = 0;
	private int miss = 0;
	private sound s;
	private boolean ingame = true;
	private boolean ref = true;
	private final String hung = "Images/Ro_hung.png";
	private final String ro = "Images/Ro.png";
	private final String shit = "Images/shit.png";
	private final String egg = "Images/egg.png";
	private String message = "Game Over";

	private Thread animator;

	public Board() {
		addKeyListener(new TAdapter());
		setFocusable(true);
		d = new Dimension(BOARD_WIDTH, BOARD_HEIGTH);
		setBackground(Color.white);

		gameInit();
		setDoubleBuffered(true);
	}

	public void addNotify() {
		super.addNotify();
		gameInit();
	}

	public void gameInit() {
		giacam_ = new ArrayList();

		getGiacam(2, "");
		// thiet lap ro hung trung
		player = new Player();
		if (animator == null || !ingame) {
			animator = new Thread(this);
			animator.start();
		}
	}

	public void drawGiacam(Graphics g) {
		Iterator it = giacam_.iterator();

		while (it.hasNext()) {
			giacam gc = (giacam) it.next();

			g.drawImage(gc.getImage(), gc.getX(), gc.getY(), this);
		}
	}

	public void drawPlayer(Graphics g) {

		if (player.isVisible()) {
			g.drawImage(player.getImage(), player.getX() + 25, player.getY(),
					this);
		}
	}

	public void drawTrung(Graphics g) {
		Iterator i3 = giacam_.iterator();
		while (i3.hasNext()) {

			giacam a = (giacam) i3.next();
			Trung t = a.getTrung();
			if (!t.isDestroyed()) {
				g.drawImage(t.getImage(), t.getX(), t.getY() + 80, this);
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		g.setColor(Color.white);
		g.fillRect(0, 0, d.width, d.height);
		g.setColor(Color.green);

		if (ingame) {

			g.drawLine(0, GROUND, BOARD_WIDTH, GROUND);
			g.drawLine(0, 100, BOARD_WIDTH, 100);
			drawGiacam(g);
			drawPlayer(g);
			drawTrung(g);
		}

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	public void gameOver() {

		Graphics g = this.getGraphics();

		g.setColor(Color.white);
		g.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGTH);

		g.setColor(new Color(0, 32, 48));
		g.fillRect(50, BOARD_WIDTH / 2 - 30, BOARD_WIDTH - 100, 50);
		g.setColor(Color.white);
		g.drawRect(50, BOARD_WIDTH / 2 - 30, BOARD_WIDTH - 100, 50);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = this.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);
		g.drawString(message, (BOARD_WIDTH - metr.stringWidth(message)) / 2,
				BOARD_WIDTH / 2);
	}

	public void animationCycle() 
	{

		// player
		player.act();

		// gia cam
		Iterator it1 = giacam_.iterator();

		while (it1.hasNext()) {
			giacam a1 = (giacam) it1.next();
			int x = a1.getX();

			// neu gia cam di chuyen het hang ngang thi di chuyen nguoc lai
			if (x >= BOARD_WIDTH - BORDER_RIGHT && direction != -1) {
				direction = -1;
				Iterator i1 = giacam_.iterator();
				while (i1.hasNext()) {
					giacam a2 = (giacam) i1.next();
				}
			}

			if (x <= BORDER_LEFT && direction != 1) {
				direction = 1;

				Iterator i2 = giacam_.iterator();
				while (i2.hasNext()) {
					giacam a = (giacam) i2.next();
				}
			}

			Iterator it = giacam_.iterator();

			while (it.hasNext()) {
				giacam gc = (giacam) it.next();
				gc.act(direction);
			}

		}
		
		Iterator i3 = giacam_.iterator();
		Random generator = new Random();
		ref = false;
		Random generator1 = new Random();
		while (i3.hasNext()) {

			int shot = generator.nextInt(150);
			int shot1 = generator1.nextInt(15);
			giacam a = (giacam) i3.next();

			// --------------bao test
			Trung b = a.getTrung();
			if (b.isShit()) {
				b = a.getTrung();
				ImageIcon ii = new ImageIcon(shit);
				b.setImage(ii.getImage());
			} else {
				b = a.getTrung();
				ImageIcon ii = new ImageIcon(egg);
				b.setImage(ii.getImage());
			}
			if (shot == CHANCE && b.isDestroyed() && ref == false) { // bao testc

				b.setDestroyed(false);
				b.setX(a.getX());
				b.setY(a.getY());

				ImageIcon ii = new ImageIcon(ro);
				player.setImage(ii.getImage());
			}
			int trungX = b.getX();
			int trungY = b.getY() + 80;
			int playerX = player.getX();
			int playerY = player.getY();

			// trung roi vao gio
			if (player.isVisible() && !b.isDestroyed() && ref == false) {
				if (trungX >= (playerX) && trungX <= (playerX + PLAYER_WIDTH)
						&& trungY >= (playerY)
						&& trungY <= (playerY + PLAYER_HEIGHT)) {
					
					    s = new sound("EggLands.wav");
					ImageIcon ii = new ImageIcon(hung);
					player.setImage(ii.getImage());
					

					if (b.isShit()) {
						die++;
					} else {
						ketqua++;
					}
					
					b.setDestroyed(true);
					if (shot1 == CHANCE) {
						b.shit();
					} else {
						b.egg();
					}
				}
			}

			// tha trung

			if (!b.isDestroyed() && ref == false) {
				b.setY(b.getY() + 1);
				// trung roi ra ngoai
				if (b.getY() + 80 >= GROUND - EGG_HEIGHT) {
					s = new sound("BreakEgg.wav");
					b.setDestroyed(true);
					if (!b.isShit()) {
						miss++;
					}
					if (shot1 == CHANCE) {
						b.shit();
					} else {
						b.egg();
					}
				}
			}

			// ---------------------
		}

	}

	public void run() {

		long beforeTime, timeDiff, sleep;

		beforeTime = System.currentTimeMillis();

		while (ingame) {
			repaint();

			animationCycle();
			
			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = DELAY - timeDiff;
			if (sleep < 0)
				sleep = 2;

			if (ketqua >= NUMBER_OF_GIAC) {
				sleep -= 5;
			}

			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				System.out.println("interrupted");
			}

			beforeTime = System.currentTimeMillis();

			if (die >= NUMBER_OF_DIE) {
				ingame = false;
			}

			if (miss >= NUMBER_OF_DIE) {
				ingame = false;
			}
			
		}
		gameOver();
	}

	public void getGiacam(int i, String images) {
		// thiet lap gia cam
		String[] loai = new String[3];
		loai[0] = "Images/ga.png";
		loai[1] = "Images/vit.png";
		loai[2] = "Images/ngan.png";

		if (i > 2) {
			ImageIcon ii = new ImageIcon(images);
			giacam gc = new giacam(giacamX + 120 * i, giacamY, images);
			gc.setImage(ii.getImage());
			giacam_.add(gc);

		} else {
			for (int j = 0; j < loai.length; j++) {
				ImageIcon ii = new ImageIcon(loai[j]);
				giacam gc = new giacam(giacamX + 120 * j, giacamY, loai[j]);
				gc.setImage(ii.getImage());
				giacam_.add(gc);
			}
		}

		Iterator it = giacam_.iterator();
		int ii = 0;
		while (it.hasNext()) {
			giacam gc = (giacam) it.next();
			gc.setX(giacamX + 100 * ii);
			ii++;
			Trung b = gc.getTrung();
			b.setDestroyed(true);
		}
		ref = true;
	}

	private class TAdapter extends KeyAdapter {
		int i = 2;

		public void keyReleased(KeyEvent e) {
			player.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {

			player.keyPressed(e);
			int key = e.getKeyCode();

			if (key == KeyEvent.VK_G || key == KeyEvent.VK_V
					|| key == KeyEvent.VK_N) {
				String images = "";

				if (key == KeyEvent.VK_G) {
					i += 1;
					images = "Images/ga.png";
				}

				if (key == KeyEvent.VK_V) {
					i += 1;
					images = "Images/vit.png";
				}
				if (key == KeyEvent.VK_N) {
					i += 1;
					images = "Images/ngan.png";
				}
				getGiacam(i, images);
			}
		}
	}
	
	

}
